﻿using Microsoft.EntityFrameworkCore;
using Services.Identity.Domain.Entities;
using Services.Identity.Infrastructure;
using SimpleRabbitmq;
using SimpleRabbitmq.Handler;

namespace Services.Identity.WebApi.EventHandlers;

[Event("VolunteerEnlistActivity")]
public class VolunteerEnlistActivityHandler(MyIdentityDbContext dbContext) : IEventJsonHandler<VolunteerEnlistActivity>
{
    private readonly MyIdentityDbContext dbContext = dbContext;
    public override async Task HandleJson(string eventName, VolunteerEnlistActivity eventData)
    {
        var result = await this.dbContext.Messages.Include(x => x.UnsentUsers).Where(o => o.ActivityId == eventData.ActivityId).ToListAsync();
        if (result.Count == 0)
        {
            return;
        }
        foreach (var item in result)
        {
            item.SetUnsentUsers([UnsentUser.Create(eventData.UserId)]);
        }
        await this.dbContext.SaveChangesAsync();
    }


}
public class VolunteerEnlistActivity
{
    public long UserId { get; set; }
    public long ActivityId { get; set; }
}